package cn.example.vip.mall.goods.service.impl;

import cn.example.vip.mall.goods.entity.Brand;
import cn.example.vip.mall.goods.mapper.BrandMapper;
import cn.example.vip.mall.goods.service.BrandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
	@Autowired
	BrandMapper brandMapper;
	
	@Override
	public List<Brand> queryList(Brand brand) {
		// 条件包装对象
		QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
		// 设置名称查询
		queryWrapper.like("name", brand.getName());
		// 设置initial查询
		queryWrapper.eq("initial", brand.getInitial());
		return brandMapper.selectList(queryWrapper);	// 执行查询
	}
	
	@Override
	public Page<Brand> queryPageList(Brand brand, Long page, Long size) {
		QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
		queryWrapper.like("name", brand.getName());
		return brandMapper.selectPage(new Page<>(page, size), queryWrapper);
	}
	
	/**
	 * 根据品牌ID集合查询品牌集合
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<Brand> queryByCategoryId(Integer categoryId) {
		// 根据 categoryId 查询 List<brandId>
		List<Integer> brands = brandMapper.queryBrandsByCategoryIds(categoryId);
		// 根据 List<brandId> 查询 List<Brand>
		if(brands != null && !brands.isEmpty()){
			return brandMapper.selectList(new QueryWrapper<Brand>().in("id", brands));
		}
		return null;
	}
	
}
